import com.baomidou.mybatisplus.core.toolkit.StringUtils;

public class Tiqu {

    public static void main(String[] args) {
        String temp = "['演员'] ['-'] ['李连杰'|'周杰伦'|'甄子丹'] [';']" +
                      "['歌手'] ['-'] ['周杰伦'|'周杰'|'周伦'] [';']" +
                      "['歌曲'] ['-'] ['青花瓷'|'理想'|'雨蝶'] [';']" +
                      "['电影'] ['-'] ['双节棍'|'青蜂侠'|'英雄'] [';']" +
                      "['作曲'] ['-'] ['方文山'|'李建'|'宁远'] [';']";

        String content = "青花瓷是周杰伦演唱的一首歌曲，由方文山作词。";
        String result = isJavaKeyword(temp, content);
        System.out.println(result);


    }

    public static String isJavaKeyword(String textTemp, String content) {
        StringBuffer s1 = new StringBuffer();
        StringBuffer s2 = new StringBuffer();

        String replaceStr = textTemp.replaceAll("\\['", "").replaceAll("\\']", "");
        String[] stringArray = replaceStr.split(";");
        for (String s : stringArray) {
            boolean b = nContrast(s.trim(), content.trim(), "青花瓷");
            if (b) {
                if(StringUtils.isNotBlank(s1)){
                    s1 = s1.append("/").append(s.substring(0, s.indexOf("-")).trim());
                }else{
                    s1 = s1.append("青花瓷/").append(s.substring(0, s.indexOf("-")).trim());
                }
            }
            boolean c = nContrast(s, content, "周杰伦");
            if (c) {
                if(StringUtils.isNotBlank(s2)){
                    s2 = s2.append("/").append(s.substring(0, s.indexOf("-")).trim());
                }else{
                    s2 = s2.append("周杰伦/").append(s.substring(0, s.indexOf("-")).trim());
                }

            }
        }
        return content.replaceAll("青花瓷",s1.toString()+" ").replaceAll("周杰伦",s2.toString()+" ");
    }

    public static boolean nContrast(String str1, String str2, String ch) {
        boolean flag = false;
        int count1 = 0, count2 = 0;
        while (str1.indexOf(ch) != -1) {
            str1 = str1.substring(str1.indexOf(ch) + 1, str1.length());
            count1++;
        }
        while (str2.indexOf(ch) != -1) {
            str2 = str2.substring(str2.indexOf(ch) + 1, str2.length());
            count2++;
        }
        if (count1 == count2)
            flag = true;
        return flag;
    }
}
